package com.example.keyboardedittext.fraction;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.keyboardedittext.R;

/**
 * Created by gongshuobo on 2016/12/14.
 */

public class FractionLayoutView extends LinearLayout implements View.OnClickListener {

    private EditText editInteger;
    private EditText editMolecular;
    private EditText editDenominator;
    private EditText currentEditText;
    private FractionView fractionView;
    private FractionCallBack fractionCallBack;


    public FractionLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        fractionView = new FractionView(context,attrs);
        initView();
    }

    public FractionLayoutView(Context context) {
        super(context);
        fractionView = new FractionView(context);
        initView();
    }

    private void initView() {

        View fractionView = LayoutInflater.from(getContext()).inflate(R.layout.fraction_layout,null);
        editInteger = (EditText) fractionView.findViewById(R.id.edit_integer);
        editMolecular = (EditText) fractionView.findViewById(R.id.edit_molecular);
        editDenominator = (EditText) fractionView.findViewById(R.id.edit_denominator);
        fractionView.findViewById(R.id.btn_confirm).setOnClickListener(this);
        editInteger.setOnClickListener(this);
        editMolecular.setOnClickListener(this);
        editDenominator.setOnClickListener(this);
        currentEditText = editInteger;
        addView(fractionView,getKeyBoardContentParams());
    }

    /** 内容区域需要的布局参数**/
    public RelativeLayout.LayoutParams getKeyBoardContentParams(){
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    }

    public EditText getCurrentEditText(){
        return currentEditText;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.edit_integer:
                currentEditText = editInteger;
                break;
            case R.id.edit_molecular:
                currentEditText = editMolecular;
                break;
            case R.id.edit_denominator:
                currentEditText = editDenominator;
                break;
            case R.id.btn_confirm:
                if(fractionCallBack!=null){
                    String strInteger = editInteger.getText().toString();
                    String strMolecular = editMolecular.getText().toString();
                    String strDenominator = editDenominator.getText().toString();
                    Bitmap fractionBitmap = fractionView.createBitmapByText(strInteger,strMolecular,strDenominator,30);
                    fractionCallBack.getFractionImg(fractionBitmap);
                    setVisibility(View.GONE);
                }
                break;
        }
    }


    public void setFractionCallBack(FractionCallBack fractionCallBack) {
        this.fractionCallBack = fractionCallBack;
    }
}
